<?php

class Prize extends CI_Model
{

    public function __construct()
    {
        // Call the CI_Model constructor
        parent::__construct();
        $this->load->database();
        date_default_timezone_set('PRC');
    }

    /**
     * 获取中奖情况
     *
     * @param $mobile
     * @return bool
     */
    public function get_prize($mobile)
    {
        $this->db->trans_start();
        $r = $this->get_prize_info();
        $counts = $this->get_user_prize_counts($mobile);
        $user = $this->get_users_nums($mobile);
        $cj_counts = !empty($user) ? $user['num'] : 1;
        if($counts >= $cj_counts || !$r){
            $this->db->trans_rollback();
            return false;
        }

        $this->db->query('insert into prize_user (mobile,prize_type,nums) values ('.$mobile.','.$r['type'].',1)');
        $this->db->query('update prize set date = "'.date('y-m-d').'" where id = '.$r['id']);
        $this->db->trans_complete();
        return $r['type'];
    }

    /**
     * 获取今天分配奖品
     *
     * @return mixed
     */
    public function get_prize_info()
    {
        $query = $this->db->query('SELECT * FROM `prize` WHERE id >= (SELECT floor(RAND() * (SELECT MAX(id) FROM `prize` where date != "'.date('Y-m-d').'"))) and date != "'.date('Y-m-d').'"
                                 ORDER BY id LIMIT 1');
        return $query->row_array();
    }

    /**
     * 用户可抽奖次数
     *
     * @param $mobile
     * @return mixed
     */
    public function get_users_nums($mobile)
    {
        $query = $this->db->get_where('user_nums', array('mobile' => $mobile));

        return $query->row_array();
    }

    /**
     * 用户中奖次数
     *
     * @param $mobile
     * @return mixed
     */
    public function get_user_prize_counts($mobile)
    {
        $query =  $this->db->get_where('prize_user', array('mobile' => $mobile));

        return $query->num_rows();
    }

    /**
     * 派奖
     *
     * @param $id
     */
    public function update_prize_user($id){
        $this->db->query('update prize_user set is_send = 1 where id ='.$id);
    }

    /**
     * @param $data
     * @return mixed
     * 保存分享后抽奖次数
     */
    public function save_user_nums($data)
    {
        if(empty($data['id']))
        {
            $r = $this->db->insert('user_nums', $data);
        }else
        {
            $this->db->where('id', $data['id']);
            unset($data['id']);
            $r = $this->db->update('user_nums', $data);
        }
        return $r ;
    }

    /**
     * 获取抽奖金额统计
     *
     * @return mixed
     */
    public function  get_prize_count($limit, $start)
    {
        $sql = "select DATE_FORMAT(create_time,'%Y-%m-%d') as date,count(*) as count,
sum(
CASE prize_type when 0 then 1   WHEN 1 then 8 when 2 then 16 else 24 end) as prize
 from prize_user   GROUP BY DATE_FORMAT(create_time,'%y-%m-%d') limit ".$start.','.$limit;

        $query = $this->db->query($sql);

        return $query->result_array();
    }

    /**
     * 获取抽奖每天总数
     *
     * @return mixed
     */
    public function  get_prize_count_all()
    {
        $sql = "select count(*) as count from prize_user GROUP BY DATE_FORMAT(create_time,'%y-%m-%d')";

        $query = $this->db->query($sql);

        return $query->num_rows();
    }

}